package com.xiguthings.waka.web.controller.userCDcard;

import com.xiguthings.waka.dao.DataBaseStatus;
import com.xiguthings.waka.exception.BussinessException;
import com.xiguthings.waka.po.*;
import com.xiguthings.waka.po.extpo.Page;
import com.xiguthings.waka.publicUtils.StringUtils;
import com.xiguthings.waka.service.userCDcard.IUserCDcardService;
import com.xiguthings.waka.web.controller.base.AbstractController;
import com.xiguthings.waka.web.controller.base.ResponseEntity;
import com.xiguthings.waka.web.controller.user.UserVo;
import com.xiguthings.waka.web.controller.useranswer.UserAnswerVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import springfox.documentation.annotations.ApiIgnore;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;

@Controller
@RequestMapping("/userCDcard")
@Api(tags = "用户的加速卡")
public class UserCDcardController extends AbstractController {
    @Resource
    private IUserCDcardService userCDcardService;


    /**
     * 创建查询条件
     *
     * @param userCDcardVo
     * @param example
     * @throws NumberFormatException
     */
    private UserCoolDownCardExample.Criteria queryConditions(UserCDcardVo userCDcardVo, UserCoolDownCardExample example) throws NumberFormatException {

        UserCoolDownCardExample.Criteria criteria = example.createCriteria();
        // 如果查询条件为空的
        if (userCDcardVo == null) {
            // 则查询所有未被删除的
            criteria.andStatusNotEqualTo(DataBaseStatus.DELETE);
            return criteria;
        }

        // 如果有查询条件
        UserCoolDownCard userCoolDownCard = userCDcardVo.getUserCDcard();
        // 添加查询条件
        if (userCoolDownCard != null) {
            if (StringUtils.isNotBlank(userCoolDownCard.getCoolDownCardId())) {
                criteria.andCoolDownCardIdEqualTo(userCoolDownCard.getCoolDownCardId());
            }
            if (StringUtils.isNotBlank(userCoolDownCard.getUserCoolDownCardId())) {
                criteria.andUserCoolDownCardIdEqualTo(userCoolDownCard.getUserCoolDownCardId());
            }
            if (StringUtils.isNotBlank(userCoolDownCard.getUserId())) {
                criteria.andUserIdEqualTo(userCoolDownCard.getUserId());
            }
        }


        if (StringUtils.isNotBlank(userCDcardVo.getInStatus())) {
            // 添加包含的状态
            String statuses = userCDcardVo.getInStatus();
            String[] statusArr = statuses.split(",");
            List<Integer> statusList = new ArrayList<>();
            for (String str : statusArr) {
                statusList.add(new Integer(str));
            }
            criteria.andStatusIn(statusList);
        } else {
            // 如果状态的查询条件是空的，则查询没有被删除的
            criteria.andStatusNotEqualTo(DataBaseStatus.DELETE);
        }

        return criteria;
    }


    /**
     * 分页查询
     *
     * @param userCDcardVo
     * @param page
     * @return
     * @throws Exception
     */

    @ApiOperation(tags = {"1.控制台",
            "2.用户端"}, value = "分页查询", httpMethod = "GET", notes = "可根据条件进行筛选", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCDcard.userCoolDownCardId", value = "主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "userCDcard.coolDownCardId", value = "加速卡的id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "userCDcard.userId", value = "所属的用户的主键id", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "page", value = "页码，如果没有值，则使用默认值。开始值从 1 开始计算", dataType = "int", required = false, paramType = "query"),
            @ApiImplicitParam(name = "rows", value = "每页的数量,如果没有值，则使用默认值", dataType = "int", required = false, paramType = "query")})
    @RequestMapping("/pagelist")
    @ResponseBody
    public ResponseEntity pageList(@ApiIgnore UserCDcardVo userCDcardVo, @ApiIgnore Page page) throws Exception {
        UserCoolDownCardExample example = new UserCoolDownCardExample();
        UserCoolDownCardExample.Criteria criteria = queryConditions(userCDcardVo, example);
        User user = (User) super.getSession().getAttribute(SESSION_CRN_USER);
        if (user != null) {
            criteria.andUserIdEqualTo(user.getUserId());
        }
        super.selectByPage(page, example, userCDcardService);
        return ajaxReturnMap(CODE_SUCCESS, null, page);
    }


    @ApiOperation(tags = {
            "2.用户端"}, value = "用户使用加速卡", httpMethod = "GET", notes = "主键必填", response = ResponseEntity.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userCDcard.userCoolDownCardId", value = "主键id", dataType = "String", required = false, paramType = "query"),
    })
    @RequestMapping("/useCDcard")
    @ResponseBody
    public ResponseEntity useCDcard(@ApiIgnore UserCDcardVo userCDcardVo) throws Exception {

        if (userCDcardVo == null || userCDcardVo.getUserCDcard() == null) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, null);
        }
        UserCoolDownCard userCoolDownCard = userCDcardVo.getUserCDcard();
        String userCoolDownCardId = userCoolDownCard.getUserCoolDownCardId();
        if (StringUtils.isBlank(userCoolDownCardId)) {
            BussinessException.throwThis(CODE_EMPTY_REQUIRED, null);
        }
        // 执行使用加速卡
        userCDcardService.useCDcardByPrimaryKey(userCoolDownCardId);
        return ajaxReturnMap(CODE_SUCCESS, "加速成功", null);
    }

}
